<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header :addTodo="addTodo"/>
      <!-- <List :todos="todos" :deleteTodo="deleteTodo" :checkOne="checkOne"/> -->
      <List :todos="todos" :deleteTodo="deleteTodo" />

      <Footer :todos="todos" :clearDone="clearDone" :checkAll="checkAll"/>
    </div>
  </div>
</template>

<script>
import {nanoid} from 'nanoid'
import Footer from './components/Footer.vue'
import Header from './components/Header.vue'
import List from './components/List.vue'
export default {
  components: { Header, List, Footer },
  name: 'App',
  data(){
    return {
        todos:[
            {
                id:nanoid(),
                title:'吃饭',
                isDone:true
            },
            {
                id:nanoid(),
                title:'睡觉',
                isDone:false

            }
        ]
    }
  },
  methods:{
    // 子组件向父组件传递数据
    // 1. 父组件定义方法
    addTodo(title){
        console.log('App title: ', title)
        // 1. 创建一个todo对象
        const todo = {
            id:nanoid(),
            title,
            isDone:false
        }
        // 2. 添加到todos数组中
        this.todos.push(todo)
        // 3. 清空表单
        window.event.target.value = ''
    },
    // 删除todo
    deleteTodo(id){
        if(!confirm('确定删除么？')) return
        this.todos = this.todos.filter(todo=>todo.id !== id)
    },
    // 方式一：根据id切换isDone属性
    // checkOne(id){
    //     console.log('App id: ', id)
    //     this.todos = this.todos.map(todo=>{
    //         if(todo.id === id){
    //             todo.isDone = !todo.isDone
    //         }
    //         return todo
    //     })
    // }
    clearDone(){
        if(!confirm('确认删除已完成么?')) return;
        this.todos = this.todos.filter(todo=>!todo.isDone)
    },
    // 全选反选
    checkAll(isDone){
        console.log('App isDone: ', isDone)
        this.todos = this.todos.map(todo=>{
            todo.isDone = isDone
            return todo
        })
    }
  },
  // 组件更新后执行
  updated(){
    console.log('updated 组件更新了')
  },
  watch:{
    todos:{
        deep:true,
        handler(value){
            console.log('watch todos发生了变化')
        }
    }
  }
}
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>